package com.nbicc.newpic.controller;

import com.nbicc.common.ResponseObj;
import com.nbicc.file.bo.FileDesc;
import com.nbicc.newpic.service.INewPicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(value = "新闻图片" , description = "新闻图片的控制操作")
@RestController
@RequestMapping(value = "/newPic")
public class NewPicController {

    @Resource(name = "NewPicServiceImpl")
    private INewPicService newPicService;

    @ApiOperation(value = "新闻图片获取" , notes = "图片id")
    @GetMapping("/{id}")
    public void downloadFile(
            @PathVariable("id") long id,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        newPicService.downloadFile(request , response , id);
    }

    @ApiOperation(value = "新闻图片上传" , notes = "上传后，返回文件描述信息")
    @PostMapping
    @ResponseBody
    public ResponseObj uploadFile(
            HttpServletRequest request,
            MultipartFile uploadFile) throws IOException {
        FileDesc fileDesc = newPicService.uploadFile(request, uploadFile);
        return ResponseObj.success().setResult(fileDesc);
    }

}
